<?php 
namespace App\Services;

use App\Facades\Infra\AdminManager;
use App\Facades\Utils\ValidateParameter;
use App\Facades\Utils\LogUtil;
use Auth;

class CaseTypeService{

	public function getAll(){
		return AdminManager::getAllCaseTypes();
	}

	public function create($request){

		$rules = [
			'type_name' => 'required|unique:case_types,type_name'
		];

		ValidateParameter::validate($request,$rules);

		$data = $request->all();

		AdminManager::createCaseType($data);

		LogUtil::createLog(Auth::user()->name,'创建了一个新案例类型：'.$data['type_name']);
	}


	public function update($request){

		$rules = [
			'id' => 'required|exists:case_types,id',
			'type_name' => 'required|unique:case_types,type_name,'.$request->id
		];

		ValidateParameter::validate($request,$rules);

		$data = $request->all();

		$id = $data['id'];

		$caseType = AdminManager::updateCaseType($id,$data);

		LogUtil::updateLog(Auth::user()->name,'修改了一个案例类型：'.$caseType->type_name.'->'.$data['type_name']);
	}

	public function delete($request){

		$rules = [
			'id' => 'required|exists:case_types,id'
		];

		ValidateParameter::validate($request,$rules);

		$data = $request->all();

		$id = $data['id'];

		$caseType = AdminManager::deleteCaseType($id);

		LogUtil::deleteLog(Auth::user()->name,'删除了一个案例类型：'.$caseType->type_name);
	}
}